<template>
	<view class="content">
		<view class="library-img">
			<image :src="'http://localhost:4567/messy_json/img?path='+libraryInfo.img" mode="aspectFit"></image>
		</view>
		<view class="library-name">
			名称：{{libraryInfo.libraryName}}
		</view>
		<view class="library-location">
			位置：{{libraryInfo.location}}
		</view>
		<view class="library-msg">
			{{libraryInfo.message}}
		</view>
		<view class="reser-list">
			<reserList :reserList="reserList" @changeReser="changeReser"></reserList>
		</view>
	</view>
</template>

<script>
	import reserList from "../../components/reser/reser.vue"
	export default {
		data() {
			return {
				id: "",
				libraryInfo: "",
				reserList: [],
				token: "",
				userInfo: ""
			};
		},
		components: {
			reserList
		},
		onLoad: function(option) {
			this.id = option.id;
			var t = this;
			uni.getStorage({
				key: 'token',
				success: function(storage) {
					t.token = storage.data
					t.getInfo(t.token.tokenName);
				},
				fail: function(res) {
					_self.error_text = "服务器错误";
				}
			});
		},
		methods: {
			getInfo(token) {
				var t = this;
				uni.request({
					url: "http://localhost:4567/library_json/info.json/" + t.id,
					header: {
						"token": token,
					},
					method: "POST",
					success(res) {
						t.libraryInfo = res.data;
						t.getReser(t.token.tokenName);
						console.log(t.libraryInfo);
					},
					fail() {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					}
				})
			},
			getReser(token) {
				var t = this;
				uni.request({
					url: "http://localhost:4567/reser_json/list.json/" + t.id,
					header: {
						"token": token,
					},
					method: "POST",
					success(res) {
						t.reserList = res.data;
						console.log(t.reserList);
					},
					fail() {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					}
				})
			},
			changeReser(reserInfo) {
				var t = this;
				uni.request({
					url: "http://localhost:4567/reser_json/reser.json/",
					header: {
						"token": t.token.tokenName,
					},
					data: {
						"user": t.token.message,
						"library": JSON.stringify(t.libraryInfo),
						"reser": JSON.stringify(reserInfo)
					},
					method: "POST",
					success(res) {
						if (res.data.code) {
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确认');
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						} else {
							uni.showModal({
								title: '提示',
								content: "预约成功",
								success: function(res) {
									if (res.confirm) {
										uni.navigateBack();
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}
					},
					fail() {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					}
				})
			},

		}
	}
</script>

<style lang="less" scoped>
	.library-img {
		width: 100%;

		image {
			width: 100%;
		}
	}

	.reser-list {
		width: 100%;
		border-top: 1px #C0C4CC solid;
		margin-top: 30rpx;
	}
</style>
